#!/usr/local/bin/python2.7
# encoding: utf-8
'''
生成游戏日志数据

@author:     user_name

@copyright:  2013 organization_name. All rights reserved.

@license:    license

@contact:    user_email
'''

import sys
import os
import datetime
import random
import robots_data
import uuid

__all__ = []
__version__ = 0.1
__date__ = '2013-12-16'
__updated__ = '2013-12-16'

curdate = datetime.datetime.now()
g_robot = None
g_timestep = 60


LANGS   = ['en_US', 'zh_CN', 'zh_HK', 'zh_TW', 'us_US', 'uk_UA', 'sv_SE', 'ru_BY', 'ro_RO']

RESS    = ['800x480', '480x320', '1280x720', '854x480', '960x540', '1280x800', '320x240', '1280x752']

MODELS  = ['GT-I9300', 'GT-N7100', 'GT-I9100', 'GT-N7000', 'SCH-I699', 'GT-I9100G', 'GT-I9308', 'MI-ONE Plus', 'MI 1S', 'MI 2']

BRANDS  = ['samsung', 'htc', 'lge', 'sony ericsson', 'lenovo', 'motorola', 'huawei', 'zte']

VERSONS = ['4.0.4', '2.3.6', '2.3.5', '4.0.3', '4.1.2', '4.1.1', '2.3.4', '2.3.7', '2.2.2', '2.3.3']

NETWORKS= ['wifi', 'CMWAP', '3gnet', 'ctwap', 'ctnet', 'CTC', 'cmnet']

CHANNELS= ['360', '91zhushou', '91cpt', 'googleandroid', 'youxin', 'amazon', 'wandoujia']

SERVERS = ['adns1', 'adns2']

PAYTYPES= ['alipay', 'sms', 'googlepay', 'amazonpay']

PRICES  = [100, 200, 500, 800, 1000, 1500, 3000, 5000, 10000]

APPS    = ['3', '2', '1001', 'zdzldtwd']

PLATS   = ['android', 'iOS']

GAMES   = ['ddd', 'zdzl']

CPNAMES = ['钻石', '金币']

def random_ip():
    return "%d.%d.%d.%d" % (random.randint(11, 254),random.randint(11, 254),random.randint(11, 254),random.randint(11, 254))

def gen_uuid():
    return uuid.uuid1().hex

def get_pay():
    payobj = {}
    payobj['price'] = random.choice(PRICES)
    payobj['buytype'] = payobj['gifttype']  = random.randint(1, 2)
    payobj['buyTypeTotal']   = payobj['giftTypeTotal'] = random.randint(100000, 999999999)
    
    if payobj['buytype'] == 2:
        payobj['buynum'] =  payobj['price'] / 10
    else:
        payobj['buynum'] =  payobj['price'] * 10
        
    payobj['buyTypeTotal']  += payobj['buynum']
    payobj['giftTypeTotal'] = payobj['buyTypeTotal']
    payobj['giftnum']       = int(payobj['buynum'] * (payobj['price'] / 10000) )
    payobj['giftTypeTotal'] += payobj['giftnum']
    payobj['buyTypeTotal']  = payobj['giftTypeTotal']
    payobj['paytype'] = random.choice(PAYTYPES)
    
    return payobj
    

def write_log(logf, logline):
    logf.write("%s\n" % logline)

"""
self.ip           = linelist[2]
self.userId       = linelist[3]
self.userName     = linelist[4]
self.gameid       = linelist[5]
self.appid        = linelist[6]
self.devid        = linelist[7]
self.channel      = linelist[8]
self.server       = linelist[9]
self.userType     = 0
"""
def gen_login(logf):
    global curdate, g_robot
    write_log(logf, "%s\t1\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s" %\
               (
                curdate.strftime("%H%M%S"), 
                random_ip(), 
                gen_uuid(),
                g_robot[0],
                random.choice(GAMES),
                random.choice(APPS),
                random.choice(PLATS),
                random.choice(CHANNELS),
                random.choice(SERVERS),
                ))

def gen_reg(logf):
    global curdate, g_robot
    write_log(logf, "%s\t1\t%s\t%s\t%d\t%s\t%s\t1000\t%s" % ( curdate.strftime("%Y%m%d%H%M%S"),random_ip(), g_robot[0],\
                                                                         random.randint(0, 1), g_robot[1],random.choice(CHANNELS), gen_uuid()))

def gen_pay(logf):
    global curdate, g_robot
    #161206    2    10.96.131.143    77377c5e58a511e3adc4bc5ff4d1879c    KTef9yHW    ddd    3    android    amazon    adns1    1000    钻石    100    100    1    gameroleid    gamerole    100    1000    gamelevel    1
    payobj = get_pay()
    write_log(logf, "%s\t2\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%d\t%s\t%d\t%d\t%d\t%s\t%s\t%d\t%d\t%d\t%d" % (
                                                                          curdate.strftime("%H%M%S"),\
                                                                          random_ip(),
                                                                          gen_uuid(),
                                                                          g_robot[0],
                                                                          gen_uuid(),
                                                                          random.choice(GAMES), 
                                                                          random.choice(APPS),
                                                                          random.choice(PLATS),
                                                                          random.choice(CHANNELS), 
                                                                          random.choice(SERVERS),
                                                                          payobj['price'],
                                                                          random.choice(CPNAMES),
                                                                          payobj['price'],
                                                                          payobj['price'],
                                                                          payobj['buynum'],
                                                                          g_robot[0],
                                                                          g_robot[1],
                                                                          payobj['price'],
                                                                          payobj['buynum'],
                                                                          random.randint(1, 200),
                                                                          random.randint(1, 10),
                                                                          ))

def get_logtime():
    global curdate,g_timestep,g_logtime
    
    g_logtime = curdate  + datetime.timedelta(seconds = random.randint(0, g_timestep) )
    g_timestep += 60


def main(): 
    global curdate, g_robot
    offday  = int(sys.argv[1]) if len(sys.argv) > 1 else 0
    now = datetime.datetime.now()
    curdate = now - datetime.timedelta(days = offday )
    
    with open("D:\\PyWork\\rdminelog\\glogs\\sdklog\\%s.%s.log" %("act.pay.daimin", curdate.strftime("%y%m%d")), 'a' ) as logf:
        for i in xrange(random.randint(100000, 555555)):
            rname = robots_data.choice_robot()
            g_logtime = get_logtime()
            g_robot  = [rname, robots_data.robots[rname][1]]
            if i % 2 ==0:
                # 登陆
                gen_login(logf)
            elif i % 3 == 0:
                #支付
                gen_pay(logf)

if __name__ == "__main__":
    main()
    
    
        